package com.ocom.exposeapi.controller;


import com.ocom.common.constant.ExposeApiUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.exposeapi.LargeScreenRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.exposeapi.feign.MjrecodeClientService;
import com.ocom.exposeapi.feign.PayConfigClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LargeScreenController {

    @Autowired
    MjrecodeClientService mjrecodeClientService;

    @Autowired
    PayConfigClientService payConfigClientService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @PostMapping(ExposeApiUrl.URL_ExposeApi_dashboard)
    public Result dashboard(@Validated @RequestBody LargeScreenRequest request) {
        Map<String, String>  ips =  getRemoteAddr();
        String ip = ips.get("unknown");

        if(ip == null || (!ip.equals("183.6.79.233") && !ip.equals("112.74.41.243") && !ip.equals("127.0.0.1"))){
            return ResultUtil.error(ResultEnum.Err_5);
        }

        String day  = request.getParam().getString("day");
          //时间传以参特殊处理
        // current 表示当天
        // yesterday 表示昨天
        if(day !=null && day.equals("current")){
            request.getParam().put("day",DateUtils.getDate());
        }

      if(day !=null && day.equals("yesterday")){
        request.getParam().put("day",DateUtils.getYesterday());
      }


        switch (request.getAction()){
            case "today_ac_total" :
                return mjrecodeClientService.todayAcTotal(request);
            case "recently_ac_total" :
                return mjrecodeClientService.recentlyAcTotal(request);
            case "today_high_frequency_ac_person" :
                return mjrecodeClientService.todayHighFrequencyAcPerson(request);
            case "total_number_person" :
                return payConfigClientService.totalNumberPerson(request);
            case "recently_ac_time" :
                return mjrecodeClientService.recentlyAcTime(request);
            case "yesterday_student_meal_rate" :
                return payConfigClientService.yesterdayStudentMealRate(request);
            default:
                return ResultUtil.error(ResultEnum.Err_4) ;
        }
    }

    public Map<String, String> getRemoteAddr() {

        String ip = httpServletRequest.getHeader("X-Forwarded-For");
        Map<String, String> map = new HashMap<String, String>();

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = httpServletRequest.getHeader("Proxy-Client-IP");
            map.put("Proxy-Client-IP", ip);
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = httpServletRequest.getHeader("WL-Proxy-Client-IP");
            map.put("WL-Proxy-Client-IP", ip);
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = httpServletRequest.getHeader("HTTP_CLIENT_IP");
            map.put("HTTP_CLIENT_IP", ip);
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = httpServletRequest.getHeader("HTTP_X_FORWARDED_FOR");
            map.put("HTTP_X_FORWARDED_FOR", ip);

        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = httpServletRequest.getRemoteAddr();
            map.put("unknown", ip);

        }

        return map;

    }

}